home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / rcs55.zip / CO.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  23KB  |  768 lines

  1. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  2.    Copyright 1990 by Paul Eggert
  3.    Distributed under license by the Free Software Foundation, Inc.
  4.  
  5. This file is part of RCS.
  6.  
  7. RCS is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. RCS is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with RCS; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. Report problems and direct all questions to:
  22.  
  23.     rcs-bugs@cs.purdue.edu
  24.  
  25. */
  26.  
  27. /*
  28.  *                     RCS checkout operation
  29.  */
  30. /*****************************************************************************
  31.  *                       check out revisions from RCS files
  32.  *****************************************************************************
  33.  */
  34.  
  35.  
  36. /* $Log: co.c%v $
  37.  * Revision 1.2  1991/08/20  07:14:15  SGP
  38.  * Ported to MSDOS using Borland C++
  39.  *
  40.  * Revision 5.6  1990/12/04  05:18:38  eggert
  41.  * Don't checkaccesslist() unless necessary.
  42.  * Use -I for prompts and -q for diagnostics.
  43.  *
  44.  * Revision 5.5  1990/11/01  05:03:26  eggert
  45.  * Fix -j.  Add -I.
  46.  *
  47.  * Revision 5.4  1990/10/04  06:30:11  eggert
  48.  * Accumulate exit status across files.
  49.  *
  50.  * Revision 5.3  1990/09/11  02:41:09  eggert
  51.  * co -kv yields a readonly working file.
  52.  *
  53.  * Revision 5.2  1990/09/04  08:02:13  eggert
  54.  * Standardize yes-or-no procedure.
  55.  *
  56.  * Revision 5.0  1990/08/22  08:10:02  eggert
  57.  * Permit multiple locks by same user.  Add setuid support.
  58.  * Remove compile-time limits; use malloc instead.
  59.  * Permit dates past 1999/12/31.  Switch to GMT.
  60.  * Make lock and temp files faster and safer.
  61.  * Ansify and Posixate.  Add -k, -V.  Remove snooping.  Tune.
  62.  *
  63.  * Revision 4.7  89/05/01  15:11:41  narten
  64.  * changed copyright header to reflect current distribution rules
  65.  * 
  66.  * Revision 4.6  88/08/09  19:12:15  eggert
  67.  * Fix "co -d" core dump; rawdate wasn't always initialized.
  68.  * Use execv(), not system(); fix putchar('\0') and diagnose() botches; remove lint
  69.  * 
  70.  * Revision 4.5  87/12/18  11:35:40  narten
  71.  * lint cleanups (from Guy Harris)
  72.  * 
  73.  * Revision 4.4  87/10/18  10:20:53  narten
  74.  * Updating version numbers changes relative to 1.1, are actually
  75.  * relative to 4.2
  76.  * 
  77.  * Revision 1.3  87/09/24  13:58:30  narten
  78.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  79.  * warnings)
  80.  * 
  81.  * Revision 1.2  87/03/27  14:21:38  jenkins
  82.  * Port to suns
  83.  * 
  84.  * Revision 4.2  83/12/05  13:39:48  wft
  85.  * made rewriteflag external.
  86.  * 
  87.  * Revision 4.1  83/05/10  16:52:55  wft
  88.  * Added option -u and -f.
  89.  * Added handling of default branch.
  90.  * Replaced getpwuid() with getcaller().
  91.  * Removed calls to stat(); now done by pairfilenames().
  92.  * Changed and renamed rmoldfile() to rmworkfile().
  93.  * Replaced catchints() calls with restoreints(), unlink()--link() with rename();
  94.  * 
  95.  * Revision 3.7  83/02/15  15:27:07  wft
  96.  * Added call to fastcopy() to copy remainder of RCS file.
  97.  *
  98.  * Revision 3.6  83/01/15  14:37:50  wft
  99.  * Added ignoring of interrupts while RCS file is renamed; this avoids
  100.  * deletion of RCS files during the unlink/link window.
  101.  *
  102.  * Revision 3.5  82/12/08  21:40:11  wft
  103.  * changed processing of -d to use DATEFORM; removed actual from
  104.  * call to preparejoin; re-fixed printing of done at the end.
  105.  *
  106.  * Revision 3.4  82/12/04  18:40:00  wft
  107.  * Replaced getdelta() with gettree(), SNOOPDIR with SNOOPFILE.
  108.  * Fixed printing of "done".
  109.  *
  110.  * Revision 3.3  82/11/28  22:23:11  wft
  111.  * Replaced getlogin() with getpwuid(), flcose() with ffclose(),
  112.  * %02d with %.2d, mode generation for working file with WORKMODE.
  113.  * Fixed nil printing. Fixed -j combined with -l and -p, and exit
  114.  * for non-existing revisions in preparejoin().
  115.  *
  116.  * Revision 3.2  82/10/18  20:47:21  wft
  117.  * Mode of working file is now maintained even for co -l, but write permission
  118.  * is removed.
  119.  * The working file inherits its mode from the RCS file, plus write permission
  120.  * for the owner. The write permission is not given if locking is strict and
  121.  * co does not lock.
  122.  * An existing working file without write permission is deleted automatically.
  123.  * Otherwise, co asks (empty answer: abort co).
  124.  * Call to getfullRCSname() added, check for write error added, call
  125.  * for getlogin() fixed.
  126.  *
  127.  * Revision 3.1  82/10/13  16:01:30  wft
  128.  * fixed type of variables receiving from getc() (char -> int).
  129.  * removed unused variables.
  130.  */
  131.  
  132.  
  133.  
  134.  
  135. #include "rcsbase.h"
  136.  
  137. static const char *getancestor P((const char*,const char*));
  138. static int buildjoin P((const char*));
  139. static int creatempty P((void));
  140. static int fixworkmode P((const char*));
  141. static int preparejoin P((void));
  142. static int rmlock P((const struct hshentry*));
  143. static int rmworkfile P((void));
  144. static void cleanup P((void));
  145.  
  146. static const char quietarg[] = "-q";
  147.  
  148. static const char *join, *versionarg;
  149. static const char *joinlist[joinlength];/* revisions to be joined    */
  150. static int exitstatus;
  151. static int forceflag, tostdout;
  152. static int lastjoin;            /* index of last element in joinlist  */
  153. static int lockflag; /* -1 -> unlock, 0 -> do nothing, 1 -> lock */
  154. static struct hshentries *gendeltas;    /* deltas to be generated    */
  155. static struct hshentry *targetdelta;    /* final delta to be generated    */
  156.  
  157. mainProg(coId, "co", "$Id: co.c%v 1.2 1991/08/20 07:14:15 SGP Exp $")
  158. {
  159.     static const char cmdusage[] =
  160.         "\nco usage: co -{flpqru}[rev] -ddate -jjoinlist -sstate -w[login] -Vn file ...";
  161.  
  162.     const char *author, *date, *rev, *state;
  163.     const char *neworkfilename;
  164.     int changelock;  /* 1 if a lock has been changed, -1 if error */
  165.     int expmode, r;
  166.     struct buf numericrev;    /* expanded revision number    */
  167.     char finaldate[datesize];
  168.  
  169.     initid();
  170.     catchints();
  171.     author = date = rev = state = nil;
  172.     bufautobegin(&numericrev);
  173.     expmode = -1;
  174.     versionarg = nil;
  175.  
  176.         while (--argc,++argv, argc>=1 && ((*argv)[0] == '-')) {
  177.                 switch ((*argv)[1]) {
  178.  
  179.                 case 'r':
  180.                 revno:  if ((*argv)[2]!='\0') {
  181.                 if (rev) warn("redefinition of revision number");
  182.                                 rev = (*argv)+2;
  183.                         }
  184.                         break;
  185.  
  186.         case 'f':
  187.             forceflag=true;
  188.             goto revno;
  189.  
  190.                 case 'l':
  191.             if (lockflag < 0) {
  192.                                 warn("-l overrides -u.");
  193.                         }
  194.             lockflag = 1;
  195.                         goto revno;
  196.  
  197.                 case 'u':
  198.             if (0 < lockflag) {
  199.                                 warn("-l overrides -u.");
  200.                         }
  201.             lockflag = -1;
  202.                         goto revno;
  203.  
  204.                 case 'p':
  205.                         tostdout=true;
  206.                         goto revno;
  207.  
  208.         case 'I':
  209.             interactiveflag = true;
  210.             goto revno;
  211.  
  212.                 case 'q':
  213.                         quietflag=true;
  214.                         goto revno;
  215.  
  216.                 case 'd':
  217.             if (date)
  218.                 redefined('d');
  219.             str2date(*argv+2, finaldate);
  220.                         date=finaldate;
  221.                         break;
  222.  
  223.                 case 'j':
  224.                         if ((*argv)[2]!='\0'){
  225.                 if (join) redefined('j');
  226.                                 join = (*argv)+2;
  227.                         }
  228.                         break;
  229.  
  230.                 case 's':
  231.                         if ((*argv)[2]!='\0'){
  232.                 if (state) redefined('s');
  233.                                 state = (*argv)+2;
  234.                         }
  235.                         break;
  236.  
  237.                 case 'w':
  238.             if (author) redefined('w');
  239.                         if ((*argv)[2]!='\0')
  240.                                 author = (*argv)+2;
  241.             else
  242.                 author = getcaller();
  243.                         break;
  244.  
  245.         case 'V':
  246.             if (versionarg) redefined('V');
  247.             versionarg = *argv;
  248.             setRCSversion(versionarg);
  249.             break;
  250.  
  251.         case 'k':    /*  set keyword expand mode  */
  252.             if (0 <= expmode) redefined('k');
  253.             if (0 <= (expmode = str2expmode(*argv+2)))
  254.                 break;
  255.             /* fall into */
  256.                 default:
  257.             faterror("unknown option: %s%s", *argv, cmdusage);
  258.  
  259.                 };
  260.         } /* end of option processing */
  261.  
  262.     if (argc<1) faterror("no input file%s", cmdusage);
  263.  
  264.         /* now handle all filenames */
  265.         do {
  266.         finptr=frewrite=NULL;
  267.     fcopy = foutptr = NULL;
  268.     ffree();
  269.  
  270.     if (!pairfilenames(argc, argv, lockflag?rcswriteopen:rcsreadopen, true, tostdout))
  271.         continue;
  272.  
  273.         /* now RCSfilename contains the name of the RCS file, and finptr
  274.          * the file descriptor. If tostdout is false, workfilename contains
  275.          * the name of the working file, otherwise undefined (not nil!).
  276.      * Also, RCSstat has been set.
  277.          */
  278.     diagnose("%s  -->  %s\n", RCSfilename,tostdout?"stdout":workfilename);
  279.  
  280.     if (!tostdout) {
  281.         if (!getworkstat()) continue; /* give up */
  282.         if (!initeditfiles(workfilename)) {
  283.             if (errno == EACCES)
  284.                 error("%s: parent directory isn't writable",
  285.                     workfilename
  286.                 );
  287.             else
  288.                 eerror(resultfile);
  289.             continue;
  290.         }
  291.     }
  292.     if (0 <= expmode)
  293.         Expand = expmode;
  294.     if (0 < lockflag  &&  Expand == VAL_EXPAND) {
  295.         error("cannot combine -kv and -l");
  296.         continue;
  297.     }
  298.  
  299.         gettree();  /* reads in the delta tree */
  300.  
  301.         if (Head==nil) {
  302.                 /* no revisions; create empty file */
  303.         diagnose("no revisions present; generating empty revision 0.0\n");
  304.                 if (!tostdout)
  305.                         if (!creatempty()) continue;
  306.                 /* Can't reserve a delta, so don't call addlock */
  307.         } else {
  308.                 if (rev!=nil) {
  309.                         /* expand symbolic revision number */
  310.             if (!expandsym(rev, &numericrev))
  311.                                 continue;
  312.         } else
  313.             switch (lockflag<0 ? findlock(false,&targetdelta) : 0) {
  314.                 default:
  315.                 continue;
  316.                 case 0:
  317.                 bufscpy(&numericrev, Dbranch?Dbranch:"");
  318.                 break;
  319.                 case 1:
  320.                 bufscpy(&numericrev, targetdelta->num);
  321.                 break;
  322.             }
  323.                 /* get numbers of deltas to be generated */
  324.         if (!(targetdelta=genrevs(numericrev.string,date,author,state,&gendeltas)))
  325.                         continue;
  326.                 /* check reservations */
  327.         changelock = 0;
  328.         if (lockflag) {
  329.             changelock =
  330.                lockflag<0 ? rmlock(targetdelta) : addlock(targetdelta);
  331.             if (changelock) {
  332.             if (changelock<0 || !checkaccesslist())
  333.                 continue;
  334.             } else {
  335.             ffclose(frewrite);  frewrite=NULL;
  336.             seteid();
  337.             ignoreints();
  338.             r = unlink(newRCSfilename);
  339.             keepdirtemp(newRCSfilename);
  340.             restoreints();
  341.             setrid();
  342.             if (r != 0) {
  343.                 eerror(RCSfilename);
  344.                 continue;
  345.             }
  346.             }
  347.         }
  348.  
  349.                 if (join && !preparejoin()) continue;
  350.  
  351.         diagnose("revision %s%s\n",targetdelta->num,
  352.              0<lockflag ? " (locked)" :
  353.              lockflag<0 ? " (unlocked)" : "");
  354.  
  355.                 /* remove old working file if necessary */
  356.                 if (!tostdout)
  357.                         if (!rmworkfile()) continue;
  358.  
  359.                 /* prepare for rewriting the RCS file */
  360.         if (changelock) {
  361.                         putadmin(frewrite);
  362.                         puttree(Head,frewrite);
  363.             aprintf(frewrite, "\n\n%s%c",Kdesc,nextc);
  364.             foutptr = frewrite;
  365.         }
  366.  
  367.                 /* skip description */
  368.                 getdesc(false); /* don't echo*/
  369.  
  370.         locker_expansion = 0 < lockflag;
  371.                 if (!(neworkfilename=buildrevision(gendeltas,targetdelta,
  372.                            tostdout,Expand!=OLD_EXPAND)))
  373.                                 continue;
  374.  
  375.         if (changelock && !nerror) {
  376.                         /* rewrite the rest of the RCSfile */
  377.                         fastcopy(finptr,frewrite);
  378.             ffclose(finptr); finptr=NULL; /*Help the file system.*/
  379.                         ffclose(frewrite); frewrite=NULL;
  380.             seteid();
  381.             if ((r = chmod(newRCSfilename, RCSstat.st_mode & ~(S_IWUSR|S_IWGRP|S_IWOTH))) == 0) {
  382.                 ignoreints();
  383.                 r = re_name(newRCSfilename,RCSfilename);
  384.                 keepdirtemp(newRCSfilename);
  385.                 restoreints();
  386.             }
  387.             setrid();
  388.             if (r != 0) {
  389.                 eerror(RCSfilename);
  390.                 error("saved in %s", newRCSfilename);
  391.                 dirtempunlink();
  392.                                 break;
  393.                         }
  394.                 }
  395.  
  396.                 if (join) {
  397.             if (!buildjoin(neworkfilename)) continue;
  398.                 }
  399.                 if (!tostdout) {
  400.             if (!fixworkmode(neworkfilename))
  401.                 continue;
  402.             ignoreints();
  403.             r = re_name(neworkfilename,workfilename);
  404.             keepdirtemp(neworkfilename);
  405.             restoreints();
  406.             if (r != 0) {
  407.                 eerror(workfilename);
  408.                 error("see %s", neworkfilename);
  409.                                 continue;
  410.                         }
  411.         }
  412.         }
  413.     if (!tostdout) diagnose("done\n");
  414.         } while (cleanup(),
  415.                  ++argv, --argc >=1);
  416.  
  417.     tempunlink();
  418.     exitmain(exitstatus);
  419.  
  420. }       /* end of main (co) */
  421.  
  422.     static void
  423. cleanup()
  424. {
  425.     if (nerror) exitstatus = EXIT_FAILURE;
  426.     if (finptr) ffclose(finptr);
  427.     if (frewrite) ffclose(frewrite);
  428.     dirtempunlink();
  429. }
  430.  
  431. #if lint
  432. #    define exiterr coExit
  433. #endif
  434.     exiting void
  435. exiterr()
  436. {
  437.     dirtempunlink();
  438.     tempunlink();
  439.     _exit(EXIT_FAILURE);
  440. }
  441.  
  442.  
  443. /*****************************************************************
  444.  * The following routines are auxiliary routines
  445.  *****************************************************************/
  446.  
  447.     static int
  448. rmworkfile()
  449. /* Function: prepares to remove workfilename, if it exists, and if
  450.  * it is read-only.
  451.  * Otherwise (file writable):
  452.  *   if !quietmode asks the user whether to really delete it (default: fail);
  453.  *   otherwise failure.
  454.  * Returns 0 on failure to get permission, -1 if there's nothing to remove,
  455.  * 1 if there is a file to remove.
  456.  */
  457. {
  458.     if (haveworkstat)      /* File doesn't exist; set by pairfilenames*/
  459.         return -1;
  460.  
  461.     if (workstat.st_mode&(S_IWUSR|S_IWGRP|S_IWOTH) && !forceflag) {
  462.         /* File is writable */
  463.         if (!yesorno(false, "writable %s exists; remove it? [ny](n): ",
  464.             workfilename
  465.         )) {
  466.         error(!quietflag && ttystdin()
  467.             ? "checkout aborted"
  468.             : "writable %s exists; checkout aborted", workfilename);
  469.         return 0;
  470.             }
  471.         }
  472.     /* Actual unlink is done later by caller. */
  473.     return 1;
  474. }
  475.  
  476.     static int
  477. fixworkmode(const char *f)
  478. {
  479.     if (
  480.         chmod(f, WORKMODE(RCSstat.st_mode,
  481.             !(Expand==VAL_EXPAND  ||  lockflag<=0 && StrictLocks)
  482.         )) < 0
  483.     ) {
  484.         eerror(workfilename);
  485.         return false;
  486.     }
  487.     return true;
  488. }
  489.  
  490.  
  491.     static int
  492. creatempty()
  493. /* Function: creates an empty working file.
  494.  * First, removes an existing working file with rmworkfile().
  495.  */
  496. {
  497.         int  fdesc;              /* file descriptor */
  498.     int s;
  499.  
  500.     if (!(s = rmworkfile()))
  501.         return false;
  502.     if (0 < s  &&  unlink(workfilename) != 0) {
  503.         eerror(workfilename);
  504.         return false;
  505.     }
  506.         fdesc=creat(workfilename,0);
  507.     if (fdesc < 0)
  508.         efaterror(workfilename);
  509.     VOID close(fdesc); /* empty file */
  510.     return fixworkmode(workfilename);
  511. }
  512.  
  513.  
  514.     static int
  515. rmlock(const struct hshentry *delta)
  516. /* Function: removes the lock held by caller on delta.
  517.  * Returns -1 if someone else holds the lock,
  518.  * 0 if there is no lock on delta,
  519.  * and 1 if a lock was found and removed.
  520.  */
  521. {       register struct lock * next, * trail;
  522.     const char *num;
  523.         struct lock dummy;
  524.         int whomatch, nummatch;
  525.  
  526.         num=delta->num;
  527.         dummy.nextlock=next=Locks;
  528.         trail = &dummy;
  529.         while (next!=nil) {
  530.         whomatch = strcmp(getcaller(), next->login);
  531.                 nummatch=strcmp(num,next->delta->num);
  532.                 if ((whomatch==0) && (nummatch==0)) break;
  533.             /*found a lock on delta by caller*/
  534.                 if ((whomatch!=0)&&(nummatch==0)) {
  535.                     error("revision %s locked by %s; use co -r or rcs -u",num,next->login);
  536.                     return -1;
  537.                 }
  538.                 trail=next;
  539.                 next=next->nextlock;
  540.         }
  541.         if (next!=nil) {
  542.                 /*found one; delete it */
  543.                 trail->nextlock=next->nextlock;
  544.                 Locks=dummy.nextlock;
  545.                 next->delta->lockedby=nil; /* reset locked-by */
  546.                 return 1; /*success*/
  547.         } else  return 0; /*no lock on delta*/
  548. }
  549.  
  550.  
  551.  
  552.  
  553. /*****************************************************************
  554.  * The rest of the routines are for handling joins
  555.  *****************************************************************/
  556.  
  557.  
  558.     static const char *
  559. addjoin(char *joinrev)
  560. /* Add joinrev's number to joinlist, yielding address of char past joinrev,
  561.  * or nil if no such revision exists.
  562.  */
  563. {
  564.     register char *j;
  565.     register const struct hshentry *d;
  566.     char terminator;
  567.     struct buf numrev;
  568.     struct hshentries *joindeltas;
  569.  
  570.     j = joinrev;
  571.     for (;;) {
  572.         switch (*j++) {
  573.         default:
  574.             continue;
  575.         case 0:
  576.         case ' ': case '\t': case '\n':
  577.         case ':': case ',': case ';':
  578.             break;
  579.         }
  580.         break;
  581.     }
  582.     terminator = *--j;
  583.     *j = 0;
  584.     bufautobegin(&numrev);
  585.     d = 0;
  586.     if (expandsym(joinrev, &numrev))
  587.         d = genrevs(numrev.string,(char*)nil,(char*)nil,(char*)nil,&joindeltas);
  588.     bufautoend(&numrev);
  589.     *j = terminator;
  590.     if (d) {
  591.         joinlist[++lastjoin] = d->num;
  592.         return j;
  593.     }
  594.     return nil;
  595. }
  596.  
  597.     static int
  598. preparejoin()
  599. /* Function: Parses a join list pointed to by join and places pointers to the
  600.  * revision numbers into joinlist.
  601.  */
  602. {
  603.     register const char *j;
  604.  
  605.         j=join;
  606.         lastjoin= -1;
  607.         for (;;) {
  608.                 while ((*j==' ')||(*j=='\t')||(*j==',')) j++;
  609.                 if (*j=='\0') break;
  610.                 if (lastjoin>=joinlength-2) {
  611.                         error("too many joins");
  612.                         return(false);
  613.                 }
  614.         if (!(j = addjoin((char *)j))) return false;
  615.                 while ((*j==' ') || (*j=='\t')) j++;
  616.                 if (*j == ':') {
  617.                         j++;
  618.                         while((*j==' ') || (*j=='\t')) j++;
  619.                         if (*j!='\0') {
  620.                 if (!(j = addjoin((char *)j))) return false;
  621.                         } else {
  622.                                 error("join pair incomplete");
  623.                                 return false;
  624.                         }
  625.                 } else {
  626.                         if (lastjoin==0) { /* first pair */
  627.                                 /* common ancestor missing */
  628.                                 joinlist[1]=joinlist[0];
  629.                                 lastjoin=1;
  630.                                 /*derive common ancestor*/
  631.                 if (!(joinlist[0] = getancestor(targetdelta->num,joinlist[1])))
  632.                                        return false;
  633.                         } else {
  634.                                 error("join pair incomplete");
  635.                                 return false;
  636.                         }
  637.                 }
  638.         }
  639.         if (lastjoin<1) {
  640.                 error("empty join");
  641.                 return false;
  642.         } else  return true;
  643. }
  644.  
  645.  
  646.  
  647.     static const char *
  648. getancestor(const char *r1, const char *r2)
  649. /* Yield the common ancestor of r1 and r2 if successful, nil otherwise.
  650.  * Work reliably only if r1 and r2 are not branch numbers.
  651.  */
  652. {
  653.     static struct buf t1, t2;
  654.  
  655.     unsigned l1, l2, l3;
  656.     const char *r;
  657.  
  658.     l1 = countnumflds(r1);
  659.     l2 = countnumflds(r2);
  660.     if ((2<l1 || 2<l2)  &&  cmpnum(r1,r2)!=0) {
  661.         /* not on main trunk or identical */
  662.         l3 = 0;
  663.         while (cmpnumfld(r1, r2, l3+1)==0 && cmpnumfld(r1, r2, l3+2)==0)
  664.         l3 += 2;
  665.         /* This will terminate since r1 and r2 are not the same; see above. */
  666.         if (l3==0) {
  667.         /* no common prefix; common ancestor on main trunk */
  668.         VOID partialno(&t1, r1, l1>2 ? (unsigned)2 : l1);
  669.         VOID partialno(&t2, r2, l2>2 ? (unsigned)2 : l2);
  670.         r = cmpnum(t1.string,t2.string)<0 ? t1.string : t2.string;
  671.         if (cmpnum(r,r1)!=0 && cmpnum(r,r2)!=0)
  672.             return r;
  673.         } else if (cmpnumfld(r1, r2, l3+1)!=0)
  674.             return partialno(&t1,r1,l3);
  675.     }
  676.     error("common ancestor of %s and %s undefined", r1, r2);
  677.     return nil;
  678. }
  679.  
  680.  
  681.  
  682.     static int
  683. buildjoin(const char *initialfile)
  684. /* Function: merge pairs of elements in joinlist into initialfile
  685.  * If tostdout is set, copy result to stdout.
  686.  * All unlinking of initialfile, rev2, and rev3 should be done by *tempunlink().
  687.  */
  688. {
  689.     struct buf commarg;
  690.     struct buf subs;
  691.     const char *rev2, *rev3;
  692.         int i;
  693.     int status;
  694.     const char *cov[8], *mergev[12];
  695.     const char **p;
  696.  
  697.     bufautobegin(&commarg);
  698.     bufautobegin(&subs);
  699.     rev2 = maketemp(0);
  700.     rev3 = maketemp(3); /* buildrevision() may use 1 and 2 */
  701.  
  702.     cov[0] = nil;
  703.     /* cov[1] setup below */
  704.     cov[2] = CO;
  705.     /* cov[3] setup below */
  706.     p = &cov[4];
  707.     if (versionarg) *p++ = versionarg;
  708.     *p++ = quietarg;
  709.     *p++ = RCSfilename;
  710.     *p = nil;
  711.  
  712.     mergev[0] = nil;
  713.     mergev[1] = nil;
  714.     mergev[2] = MERGE;
  715.     mergev[3] = mergev[5] = "-L";
  716.     /* rest of mergev setup below */
  717.  
  718.         i=0;
  719.         while (i<lastjoin) {
  720.                 /*prepare marker for merge*/
  721.                 if (i==0)
  722.             bufscpy(&subs, targetdelta->num);
  723.         else {
  724.             bufscat(&subs, ",");
  725.             bufscat(&subs, joinlist[i-2]);
  726.             bufscat(&subs, ":");
  727.             bufscat(&subs, joinlist[i-1]);
  728.         }
  729.         diagnose("revision %s\n",joinlist[i]);
  730.         bufscpy(&commarg, "-p");
  731.         bufscat(&commarg, joinlist[i]);
  732.         cov[1] = rev2;
  733.         cov[3] = commarg.string;
  734.         if (runv(cov))
  735.             goto badmerge;
  736.         diagnose("revision %s\n",joinlist[i+1]);
  737.         bufscpy(&commarg, "-p");
  738.         bufscat(&commarg, joinlist[i+1]);
  739.         cov[1] = rev3;
  740.         cov[3] = commarg.string;
  741.         if (runv(cov))
  742.             goto badmerge;
  743.         diagnose("merging...\n");
  744.         mergev[4] = subs.string;
  745.         mergev[6] = joinlist[i+1];
  746.         p = &mergev[7];
  747.         if (quietflag) *p++ = quietarg;
  748.         if (lastjoin<=i+2 && tostdout) *p++ = "-p";
  749.         *p++ = initialfile;
  750.         *p++ = rev2;
  751.         *p++ = rev3;
  752.         *p = nil;
  753.         status = runv(mergev);
  754.         if (!WIFEXITED(status) || 1<WEXITSTATUS(status))
  755.             goto badmerge;
  756.                 i=i+2;
  757.         }
  758.     bufautoend(&commarg);
  759.     bufautoend(&subs);
  760.         return true;
  761.  
  762.     badmerge:
  763.     nerror++;
  764.     bufautoend(&commarg);
  765.     bufautoend(&subs);
  766.     return false;
  767. }
  768.